﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestureSaver;

namespace Experimenter
{
    class Algorithm0 : IAlgorithm
    {
        GestureDB trainingDatabase;

        public void Train(GestureDB trainDB)
        {
            this.trainingDatabase = trainDB;

            Common.CenterizeAllGestures(trainingDatabase);
            Common.NormalizeAllShoulderLengths(trainingDatabase);
        }

        public GestureType Recognize(Gesture g)
        {
            Common.CenterizeGesture(g);
            Common.NormalizeShoulderLength(g);

            double minCost = Double.PositiveInfinity;
            GestureType minCostType = GestureType.UnknownGestureType;

            for (int i = 0; i < trainingDatabase.TotalClassCount; i++)
            {
                double[] costs = new double[trainingDatabase[i].Count];

                for (int j = 0; j < trainingDatabase[i].Count; j++)
                {
                    costs[j] = Common.DTW(g, trainingDatabase[i][j]);
                }

                Array.Sort(costs);
                double medianCost = costs[0];

                if (medianCost < minCost)
                {
                    minCost = medianCost;
                    minCostType = trainingDatabase[i][0].gestureType;
                }
            }

            return minCostType;
        }
    }
}
